<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');

if ( !function_exists('page_config')) {
	/**
	 * 分页配置 for CI
	 * @param per 显示数目
	 * @param row 总数
	 * @param url 地址
	 * @param uri int page所在uri位置
	 **/
	function page_config($per=0,$rows=0,$url="",$uri=3){
		$config['base_url'] = $url;
		$config['total_rows'] = $rows;
		$config['per_page'] = $per;
		if ($uri != 3) {
			$config['uri_segment'] = $uri;
		}
		$config['use_page_numbers'] = TRUE;
		$config['full_tag_open'] = '<div class="pagination"><ul>';
		$config['full_tag_close'] = '</ul></div>';
		$config['first_link'] = '首页';
		$config['first_tag_open'] = '<li>';
		$config['first_tag_close'] = '</li>';
		$config['last_link'] = '末页';
		$config['last_tag_open'] = '<li>';
		$config['last_tag_close'] = '</li>';
		$config['prev_link'] = '上一页';
		$config['prev_tag_open'] = '<li>';
		$config['prev_tag_close'] = '</li>';
		$config['next_link'] = '下一页';
		$config['next_tag_open'] = '<li>';
		$config['next_tag_close'] = '</li>';
		$config['num_tag_open'] = '<li>';
		$config['num_tag_close'] = '</li>';
		$config['cur_tag_open'] = '<li class="active"><a href="#">';
		$config['cur_tag_close'] = '</a></li>';
		$config['use_point'] = TRUE;
		return $config;
	}

	// 前端使用，不用ul了
	function page_config_site($per=0,$rows=0,$url="",$uri=3){
		$config['base_url'] = $url;
		$config['total_rows'] = $rows;
		$config['per_page'] = $per;
		if ($uri != 3) {
			$config['uri_segment'] = $uri;
		}
		$config['use_page_numbers'] = TRUE;
		$config['full_tag_open'] = '<div class="pagination">';
		$config['full_tag_close'] = '</div>';
		$config['first_link'] = '首页';
		$config['first_tag_open'] = '';
		$config['first_tag_close'] = '';
		$config['last_link'] = '末页';
		$config['last_tag_open'] = '';
		$config['last_tag_close'] = '';
		$config['prev_link'] = '上一页';
		$config['prev_tag_open'] = '';
		$config['prev_tag_close'] = '';
		$config['next_link'] = '下一页';
		$config['next_tag_open'] = '';
		$config['next_tag_close'] = '';
		$config['num_tag_open'] = '';
		$config['num_tag_close'] = '';
		$config['cur_tag_open'] = '<a class="active" href="#">';
		$config['cur_tag_close'] = '</a>';
		$config['use_point'] = TRUE;
		return $config;
	}

	// 分页 for 前端
	function _pages($url,$per_page=-1,$cont=false,$uri=3){
		$CI =& get_instance();
		if (!$per_page < 0) {
			$per_page = $this->page_limit;
		}
		$CI->pagination->initialize(page_config_site($per_page,$cont,$url,$uri));
		return $CI->pagination->create_links();
	}
}

if ( ! function_exists('tag_span_color')) {
	/**
	 * 分割tags style
	 * @param $tags String
     * @return string
	 **/
	function tag_span_color($tags){
		$t=explode(',',$tags);
		$str = '';
		foreach($t as $v){
			$str .= "<span class='label' style='color:#fafafa;background-color:#".rand(100,999).";'>".$v."</span> ";
		}
		return $str;
	}
}

if (!function_exists('static_file')) {
	// TODO:BUG 当远程分离的时候，可能并不在同一个主机上，修改验证文件方式。
	/**
	 * @brief 加载js/css 判断是否加载 .min. 文件
	 *
	 * @param $file
	 * @param bool $rurl false ,值为true,1返回为地址，非html
	 * 当为css规则 screen,print 是为link修改media值
	 * 非js/css自动返回路径
	 *
	 * @return false or html code
	 */
	function static_file($file,$rurl=false){

		if (!$file) {
			return FALSE;
		}

		$type = false;
		if (strrpos($file,'.js')) {
			$filemin = str_replace('.js','.min.js',$file);
			$type = 'js/';
		}else if(strrpos($file,'.css')){
			$filemin = str_replace('.css','.min.css',$file);
			$type = 'css/';
		}

		$url = false;

		if (is_file(STATIC_PATH.$file)) {
			$url = STATIC_URL.$file;
		}

		if ($type != false) {
			if (file_exists(STATIC_PATH.$type.$file)) {
				$url  = STATIC_URL.$type.$file;
			}
			if (defined('ENVIRONMENT') and ENVIRONMENT != "development" ) {
				if (file_exists(STATIC_PATH.$type.$filemin)) {
					$url = STATIC_URL.$type.$filemin;
				}
				if (!$url and file_exists(STATIC_PATH.$file)) {
					$url = STATIC_URL.$file;
				}
			}
		}

		if (!$url) {
			return '<!-- static file error: '.$file.' findout. --><script>console.error("cont find `'.$file.'`!")</script>';
			// return false;
		}else{
			if ( defined('STATIC_V') and STATIC_V) {
				$url.='?v='.STATIC_V;
			}
			if ( defined('ENVIRONMENT') and ENVIRONMENT == "development" ) {
				//$url.='?t='.time();
			}
		}

		if ($rurl === true or $rurl and  !in_array($rurl,array('screen','print'))) {
			return $url;
		}else{
			if ($type == 'js/') {
				return '<script src="'.$url.'" type="text/javascript" charset="utf-8"></script>';
			}else if($type == 'css/'){
				$media = in_array($rurl,array('screen','print'))? $rurl:'screen';
				return '<link rel="stylesheet" href="'.$url.'" type="text/css" media="'.$media.'" charset="utf-8">';
			}else{
				return $url;
			}
		}

	}
}

if (!function_exists('sendPHPMailer')) {
	/**
	 * @param string $address 发送邮箱地址
	 * @param string $subject 发送主题
	 * @param string $body 发送正文
	 * @param string $secure 加密方式(一般465端口需要SSL 加密 25等其它端口 这里传空''字符串)
	 * @param int $debug 是否开启调试信息   1-开启 0-关闭  线上请设置为0
	 * $param file $file 文件
	 */
	function sendPHPMailer($address, $subject, $body, $secure = 'ssl', $debug = 0, $file = false)
	{
		require_once(LIBS_PATH . 'libraries/PHPMailer-master/class.phpmailer.php');
		$CI =& get_instance();
		$mail = new PHPMailer(true);
		$mail->IsSMTP();
		$mail->CharSet = "utf-8";
		$mail->SMTPDebug = (int)$debug;                     // enables SMTP debug information
		$mail->SMTPAuth = true;                  // enable SMTP authentication
		$mail->SMTPSecure = $secure;                 // sets the prefix to the servier
		//获取数据库中存的邮件配置项目
		$config = $CI->db->select('value')->where(array('category' => 'email'))->order_by('id asc')->get('configs')->result_array();
		$mail->Host = $config[0]['value'];
		$mail->Port = $config[3]['value'];
		$mail->Username = $config[1]['value'];
		$mail->Password = $config[2]['value'];
		//设置内容
		try {
			$mail->addAddress($address);
			// $mail->addAddress('18062020342@163.com');
			$mail->setFrom($mail->Username, $subject);
			$mail->Subject = $subject;
			$mail->Body = $body;
			if(file_exists($file)){
				$ext=end(explode('.',$file));
				$mail->addAttachment($file, $subject.'.'.$ext);
			}
			$mail->isHTML(true);
			$mail->send();
			return true;
		} catch (phpmailerException $e) {
			logfile('发送给:'.$address.$e->getMessage(),'email');
			//echo $e->errorMessage(); //Pretty error messages from PHPMailer
			return false;
		} catch (Exception $e) {
			logfile('发送给:'.$address.$e->getMessage(),'email');
			//echo $e->getMessage(); //Boring error messages from anything else!
			return false;
		}
	}
}



if (!function_exists('upload_file')) {
	/**
	 * 对upload文件url封装
	 */
	function upload_file($url){
		return UPLOAD_URL.$url;
	}
}

if (!function_exists('uri2get')) {

	/**
	 * @brief 将ci中的 uri参数提交为类GET的以键值p开头的变量组
	 * @param bool $merge 设定是否添加到 $_GET中
	 * @return array
	 */
	function uri2get($merge=false){
		$CI =& get_instance();
		$uri = $CI->uri->rsegment_array();
		array_shift($uri);
		array_shift($uri);
		$arr = false;
		if ($uri) {
			foreach($uri as $k=>$u){
				// p 意为 params
				$arr['p'.$k] = $u;
			}
			if ($merge) {
				$_GET = array_merge($_GET,$arr);
			}
		}
		return $arr;
	}
}

if (! function_exists('get_ip')) {
	/**
	 * Get IP 获得IP地址
	 *
	 * @access	protected
	 * @return	string
	 */
	function get_ip()
	{
		$lip = (isset($_SERVER['HTTP_X_REAL_IP']) AND $_SERVER['HTTP_X_REAL_IP'] != "") ? $_SERVER['HTTP_X_REAL_IP'] : FALSE;
		$cip = (isset($_SERVER['HTTP_CLIENT_IP']) AND $_SERVER['HTTP_CLIENT_IP'] != "") ? $_SERVER['HTTP_CLIENT_IP'] : FALSE;
		$rip = (isset($_SERVER['REMOTE_ADDR']) AND $_SERVER['REMOTE_ADDR'] != "") ? $_SERVER['REMOTE_ADDR'] : FALSE;
		$fip = (isset($_SERVER['HTTP_X_FORWARDED_FOR']) AND $_SERVER['HTTP_X_FORWARDED_FOR'] != "") ? $_SERVER['HTTP_X_FORWARDED_FOR'] : FALSE;

		if ($lip && $cip && $rip)	$IP = $lip;
		elseif ($lip)		        $IP = $lip;
		elseif ($rip)		        $IP = $rip;
		elseif ($cip)		        $IP = $cip;
		elseif ($fip)		        $IP = $fip;

		if (strpos($IP, ',') !== FALSE)
		{
			$x = explode(',', $IP);
			$IP = end($x);
		}

		if ( ! preg_match( "/^[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}$/", $IP))
		{
			$IP = '0.0.0.0';
		}

		unset($cip);
		unset($rip);
		unset($fip);

		return $IP;
	}
}

if (!function_exists('get_adress')) {
	/**
	 * 从百度获得物理地址
	 * @param  string $ip ip地址
	 * @return array     百度原始数据
	 */
	function get_adress($ip='')
	{
		if (!$ip or ! preg_match( "/^[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}$/", $ip)) {
			return false;
		}

		# 百度获得
		# http://developer.baidu.com/map/ip-location-api.htm
		# 申请 `ak` 并替换下面的`ak`
		$url = 'http://api.map.baidu.com/location/ip?ak=F454f8a5efe5e577997931cc01de3974&ip='.$ip.'&coor=bd09ll';
		return json_decode(file_get_contents($url),TRUE);
	}

	/**
	 * @brief 获取IP地理位置
	 * 淘宝
	 * @param $ip IP地址
	 * @return array
	 */
	function get_adress_tb($ip)
	{
		$url="http://ip.taobao.com/service/getIpInfo.php?ip=".$ip;
		$ip=json_decode(file_get_contents($url),TRUE);
		if((string)$ip['code']=='1'){
			return false;
		}
		$data = (array)$ip['data'];
		return $data;
	}

	// 新浪
	function get_adress_sina($ip){
	    $url='http://int.dpool.sina.com.cn/iplookup/iplookup.php?format=json&ip='.$ip;
	    $data = json_decode(file_get_contents($url),TRUE);
	    if(!empty($data)){
	    	return $data;
	    }else{
	    	return $ip;
	    }
	}
}

if (!function_exists('get_weather')) {
	function get_weather($code='101010100'){
        // $this->load->library('Curl') ;
        // $weather = $this->curl->simple_get('http://www.weather.com.cn/data/cityinfo/101010100.html');
        return file_get_contents('http://www.weather.com.cn/data/cityinfo/'.$code.'.html');
        // $weather = json_decode(file_get_contents('http://www.weather.com.cn/data/cityinfo/101010100.html'),TRUE);
        // return $weather;
    }
}

if (!function_exists('object2array')) {
	/**
	* @brief Object TO array
	* 用户 json_decode 转换的object
	* PHP 5.3 json_decode($json,true)
	*
	* @param $object
	*
	* @return $array
	*/
	function object2array($array)
	{
		if(is_object($array))
		{
			$array = (array)$array;
		}
		if(is_array($array))
		{
			foreach($array as $key=>$value)
			{
				$array[$key] = object2array($value);
			}
		}
		return $array;
	}
}

if (!function_exists('get_QR')) {
    /**
     * google api 二维码生成
     * QRcode可以存储最多4296个字母数字类型的任意文本，具体可以查看二维码数据格式
     * @param string $chl 二维码包含的信息
     * 可以是数字、字符、二进制信息、汉字。不能混合数据类型，数据必须经过UTF-8 URL-encoded.如果需要传递的信息超过2K个字节，请使用POST方式
     * @param int $widhtHeight 生成二维码的尺寸设置
     * @param string $EC_level 可选纠错级别，QR码支持四个等级纠错，用来恢复丢失的、读错的、模糊的、数据。
     * 						   L-默认：可以识别已损失的7%的数据
     * 						   M-可以识别已损失15%的数据
     * 						   Q-可以识别已损失25%的数据
     * 						   H-可以识别已损失30%的数据
     * @param int $margin 生成的二维码离图片边框的距离
     */
    function get_QR($chl,$widhtHeight ='150',$EC_level='L',$margin='0')
    {
        $chl = urlencode($chl);
        echo '<img src="http://chart.apis.google.com/chart?chs='.$widhtHeight.'x'.$widhtHeight.'&cht=qr&chld='.$EC_level.'|'.$margin.'&chl='.$chl.'" alt="QR code" widhtHeight="'.$widhtHeight.'" widhtHeight="'.$widhtHeight.'" class="box-shadow"/> ';
    }
}

if (!function_exists('is_ajax()')) {
    /**
     * 判定请求是否为ajax ,判定输入头
     * @return	boolean
     */
    function is_ajax(){
        return (isset($_SERVER['HTTP_X_REQUESTED_WITH']) && $_SERVER['HTTP_X_REQUESTED_WITH']=="XMLHttpRequest");
    }
}

if (!function_exists('is_post()')) {
	// 判定请求是否为post
	function is_post(){
		if ($_SERVER['REQUEST_METHOD'] == "POST") {
			return TRUE;
		}else{
			return FALSE;
		}
	}
}

if (!function_exists('is_get()')) {
	// 判定请求时否为get
	function is_get(){
		if ($_SERVER['REQUEST_METHOD'] == "GET") {
			return TRUE;
		}else{
			return FALSE;
		}
	}
}

/**
 * @brief json数据输出
 * @param $data array
 * @return echo
 */
/**
 * 格式化数据为json结构体
 * @param  array|object $data
 * @return string/json
 */
function json_echo($data)
{
	header('Vary: Accept');
	if (isset($_SERVER['HTTP_ACCEPT']) && (strpos($_SERVER['HTTP_ACCEPT'], 'application/json') !== false)) {
		header('Content-type: application/json');
	} else {
		header('Content-type: text/plain');
	}
	// header('Content-type: application/json');
	if (is_array($data)) {
		echo json_encode($data);
	}else{
		echo $data;
	}
}


/**
 * @brief 加密密码 连接 HMAC 字符串后 md5 加密
 * @param $pwd string 明文密码
 * @return 单向加密字符串
 */
function passwd($pwd)
{
    return md5($pwd.HMACPWD);
}

/**
 * 输出以 timeline 为基准的 加密字段
 */
function token($timeline)
{
	return md5($timeline.HMAC);
}

// for debug
function logfile($str,$fileName='api')
{
	$log = date('Ymd', time());
	if ($fileName) {
		
		// 创建对应的log文件夹
		if (!is_dir(ROOT.'logs/'.$fileName)) mkdir(ROOT.'logs/'.$fileName, 0777); 
		if (!is_dir(ROOT.'logs/'.$fileName.'/'.$log)) mkdir(ROOT.'logs/'.$fileName.'/'.$log, 0777); 
	} else {
		if (!is_dir(ROOT.'logs/'.$log)) mkdir(ROOT.'logs/'.$log, 0777); 
	}

	$log = $fileName.'/'.date('Ymd/H', time());
	$logfile=fopen(ROOT.'logs/'.$log.'.log',"a+");
	if (is_array($str)) {
		$str = print_r($str,TRUE);
	}
	fwrite($logfile,"\r\n [".date("Y-m-d H:i:s",time())."] ".$str);
	fclose($logfile);
}

/**
 * 邮件发送（修正乱码问题）
 * @param  [type] $mail [description]
 * @return [type]       [description]
 */
function smtp_sendmail($mail){

	$subject = $mail['subject'];
	$message = $mail['message'];
	$to = $mail['to'];  // array or ,

	$CI =& get_instance();
	$CI->load->library('email');
	$config['protocol'] = 'smtp';
	// $config['mailpath'] = '/usr/sbin/sendmail';
	$config['smtp_host']  = $CI->mcfg->get('email','stmp');
	$config['smtp_user']  = $CI->mcfg->get('email','account');
	$config['smtp_name']  = $CI->mcfg->get('email','name');
	$config['smtp_pass']  = $CI->mcfg->get('email','pwd');
	$config['smtp_port']  = $CI->mcfg->get('email','port');
	$config['charset'] = 'utf-8';//'iso-8859-1';
	$config["validate"] = true;
	$config["priority"] = 1;
	$config['crlf'] = "\r\n";
	$config['newline'] = "\r\n";
	$config['wordwrap'] = TRUE;
	$config['mailtype'] = 'html';
	$CI->email->initialize($config);
	$CI->email->from($config['smtp_user'], $config['smtp_name']);
	$CI->email->to($to);
	$CI->email->subject($subject);
	$CI->email->message($message);
	$CI->email->send();
	// var_dump($CI->email->print_debugger());
}

// 随机
function rand_str($length = 4, $chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz1234567890')
{
    $chars_length = (strlen($chars) - 1);
    $string = $chars{rand(0, $chars_length)};
    for ($i = 1; $i < $length; $i = strlen($string))
    {
        $r = $chars{rand(0, $chars_length)};
        if ($r != $string{$i - 1}) $string .=  $r;
    }
    return $string;
}

// 浏览器判定
function userBrowser(){
    $user_OSagent = $_SERVER['HTTP_USER_AGENT'];
    if(strpos($user_OSagent,"Maxthon") && strpos($user_OSagent,"MSIE")) {
        $visitor_browser ="Maxthon(Microsoft IE)";
    }elseif(strpos($user_OSagent,"Maxthon 2.0")) {
        $visitor_browser ="Maxthon 2.0";
    }elseif(strpos($user_OSagent,"Maxthon")) {
        $visitor_browser ="Maxthon";
    }elseif(strpos($user_OSagent,"MSIE 11.0")) {
        $visitor_browser ="MSIE 11.0";
    }elseif(strpos($user_OSagent,"MSIE 10.0")) {
        $visitor_browser ="MSIE 10.0";
    }elseif(strpos($user_OSagent,"MSIE 9.0")) {
        $visitor_browser ="MSIE 9.0";
    }elseif(strpos($user_OSagent,"MSIE 8.0")) {
        $visitor_browser ="MSIE 8.0";
    }elseif(strpos($user_OSagent,"MSIE 7.0")) {
        $visitor_browser ="MSIE 7.0";
    }elseif(strpos($user_OSagent,"MSIE 6.0")) {
       $visitor_browser ="MSIE 6.0";
    }elseif(strpos($user_OSagent,"MSIE 5.5")) {
        $visitor_browser ="MSIE 5.5";
    }elseif(strpos($user_OSagent,"MSIE 5.0")) {
        $visitor_browser ="MSIE 5.0";
    }elseif(strpos($user_OSagent,"MSIE 4.01")) {
        $visitor_browser ="MSIE 4.01";
    }elseif(strpos($user_OSagent,"MSIE")) {
        $visitor_browser ="MSIE top";
    }elseif(strpos($user_OSagent,"NetCaptor")) {
        $visitor_browser ="NetCaptor";
    }elseif(strpos($user_OSagent,"Netscape")) {
        $visitor_browser ="Netscape";
    }elseif(strpos($user_OSagent,"Chrome")) {
        $visitor_browser ="Chrome";
    }elseif(strpos($user_OSagent,"Lynx")) {
        $visitor_browser ="Lynx";
    }elseif(strpos($user_OSagent,"Opera")) {
        $visitor_browser ="Opera";
    }elseif(strpos($user_OSagent,"Konqueror")) {
        $visitor_browser ="Konqueror";
    }elseif(strpos($user_OSagent,"Mozilla/5.0")) {
        $visitor_browser ="Mozilla";
    }elseif(strpos($user_OSagent,"Firefox")) {
        $visitor_browser ="Firefox";
    }elseif(strpos($user_OSagent,"U")) {
        $visitor_browser ="Firefox";
    }else {
        $visitor_browser ="other";
    }
    return $visitor_browser;
}

// 小于 IE 9
function ielt9()
{
    $user_OSagent = $_SERVER['HTTP_USER_AGENT'];
    if (strpos($user_OSagent,"MSIE 8.0") OR strpos($user_OSagent,"MSIE 7.0") OR strpos($user_OSagent,"MSIE 6.0") ) {
    	return TRUE;
    }else{
    	return FALSE;
    }
}

if (!function_exists('dump')) {
	// 调试输出
	//
	/**
	 * 利用FirePHP调试
	 * http://pan.baidu.com/s/1jGkRmcQ
	 * @param  alltype $val     输出内容
	 * @param  string $label   标题标志 (注：为ajax时候表示异步时候调试打开)
	 * @param  string $do      Fire PHP的方法 默认log,info,warn,table,error
	 * @param  array  $options 其他参数
	 * @return noting
	 */
	function dump($val,$label=null,$do='info',$options = array()){
		if (ENVIRONMENT == 'development' OR ENVIRONMENT == 'testing') {
			if (is_ajax() AND $label == "ajax") {
				print_r($val);
				return TRUE;
			}

			$CI =& get_instance();
			if (!isset($CI->firephp)) {
				$CI->load->library('FirePHP');
				$CI->firephp->setOptions(array(
					'includeLineNumbers' => false
				));
			}
			switch ($do) {
				case 'log':
					$CI->firephp->log($val,$label,$options);
					break;
				case 'info':
					$CI->firephp->info($val,$label,$options);
					break;
				case 'warn':
					$CI->firephp->warn($val,$label,$options);
					break;
				case 'error':
					$CI->firephp->error($val,$label,$options);
					break;
				case 'table':
					$CI->firephp->table($label,$val,$options);
					break;
				case 'trace':
					$CI->firephp->trace($label);
					break;
				default:
					$CI->firephp->log($val,$label,$options);
					break;
			}
			// 关闭
			// $this->firephp->setEnabled(FALSE);
		}
		return TRUE;
	}
}

if (!function_exists('page_profiler')) {
// 查看性能信息
function page_profiler(){
	if (!isset($CI)) {
		$CI =& get_instance();
	}
	if (!isset($CI->firephp)) {
		$CI->load->library('profiler');
	}
	return $CI->profiler->run();
}
}

// API拦截以及验证
function apiValidate($data) {
	if (is_post()) {
        //验证数据
        //TODO .. 拦截数据请求

        // 加载数据加密模块
        if (!isset($CI)) {
			$CI =& get_instance();
		}
        $CI->load->library('AES');

        // 判断content
        if (isset($data['content'])) {
            
            $content = AES::decrypt($data['content'], KEY);
            $content = json_decode($content,1);


            // 解密前打印日志
            logfile('解密前数据:'.$data['content']);
            
            // 过滤数据
            if ($content) {
                //全局赋值
                $data = $content;
                $rules = array(
		            array(
		                "field" => "terminalNo",
		                "label" => "手机终端类型",
		                "rules" => "trim|required|numeric|max_length[1]|regex_match[/^[1234]*$/]"
		            ),
		            array(
		                "field" => "token",
		                "label" => "手机的token值",
		                "rules" => "trim|required|max_length[50]"
		            ),
		            array(
		                "field" => "usId",
		                "label" => "用户登录账号",
		                "rules" => "trim|numeric|max_length[11]"
		            )
			        
		        );

		        // 验证
		        $CI->form_validation->set_rules($rules);
		        // validate验证结果
		        if ($CI->form_validation->run('', $content) == FALSE) {
		            
		            // 返回失败
		            return FALSE;

		        } 
		        // 解密后打印日志
            	logfile('解密后数据:'.print_r($content, 1));
                
		        return $content;
            } else {
                
                //返回失败
                return FALSE;
            }
        }

    } else {

        //返回失败
        return FALSE;
    }
}

// 输出日期编号 生产唯一编号
// @param $insert  插入内容
function ordernum_time($insert='',$noline=FALSE)
{
    $base = date('ymdHis', time());
    if ($insert) {
        return $base.$insert.nanoSecond();
    }else{
        if ($noline) {
            return $base.nanoSecond();
        }else{
            return $base.'-'.nanoSecond();
        }
    }
}


// 接口base64保存img
function apiImgSave($base64Data=false, $fileName=false, $dir='', $w=400, $h=400){

    if (!$base64Data or !$fileName) {
        return false;
    }

    if ($dir and !strstr($dir, '/')) {
        
        $dir .= '/';
    } else if(strstr($dir, '/')) {

    	$dirArr = explode('/', $dir);
    	$dirTmp = '';
    	foreach ($dirArr as $k => $d) {

    		$dirTmp .= $d.'/';
    		if (!is_dir(UPLOAD_PATH.$dirTmp)) mkdir(UPLOAD_PATH.$dirTmp, 0777);
    		if ($k == (count($dirArr) - 1)) if (!is_dir(UPLOAD_PATH.$dirTmp.'thumbnail/')) mkdir(UPLOAD_PATH.$dirTmp.'thumbnail/', 0777);
    	}
    	$dir = $dirTmp;
    }
    if (preg_match('/^(\[removed\])/', $base64Data, $result)
    or preg_match('/^(data:\s*image\/(\w+);base64,)/', $base64Data, $result)) {
    	
    	$type = isset($result[2]) ? $result[2] : 'jpg';
	    $fileName .= '.'.$type;
	    $img = base64_decode(str_replace($result[1], '', $base64Data));
	    $data = array(
	            'name'     => $fileName,
	            'size'     => strlen($img),
	            'url'      => $dir.$fileName,
	            'type'     => $type,
	            'timeline' => time(),
	            'thumb'    => $dir.'thumbnail/'.$fileName,
	    );

	    $filePath  = UPLOAD_PATH.$dir.$fileName;
	    $filePatht = UPLOAD_PATH.$dir.'thumbnail/'.$fileName;
	    if (!is_dir(UPLOAD_PATH.$dir)) mkdir(UPLOAD_PATH.$dir, 0777); 
	    if (!is_dir(UPLOAD_PATH.$dir.'thumbnail/')) mkdir(UPLOAD_PATH.$dir.'thumbnail/', 0777); 

	    if (file_put_contents($filePath, $img) and file_put_contents($filePatht, $img)) {
	        
	        $CI =& get_instance();
	        $CI->load->library('image_lib');
	        $config['quality'] = '100';
	        $config['width'] = $w;
	        $config['height'] = $h;
	        $config['master_dim'] = 'width';
	        if (preg_match('/(gif|png|jpg|jpeg)$/i', $filePatht) ) {
	          $config['source_image'] =  $filePatht;
	          $CI->image_lib->initialize($config);
	          $CI->image_lib->resize();
	        }
    		$CI->db->insert('upload',$data);
    		if ($CI->db->affected_rows()) {
				return $CI->db->insert_id();
			}
			return 0;
	    } else {

	    	logfile('打赏任务图片数据写入失败'.$filePath.'===='.$filePatht, 'rewardTask_error');
	        return false;
	    }
    } else {
	    
	    logfile('打赏任务图片数据有错', 'rewardTask_error');
    	return false;
	}
}

/**
 * 复制数据到对象属性
 * @param type $data
 * @param type $obj
 */

if (!function_exists('copyDataToClass')) {  
    function copyDataToClass($data, $obj) {  
        if (empty($data) || empty($obj)) {
            return '';
        }

        $src_vars = is_object($data) ? get_object_vars($data) : $data;
        foreach ($src_vars as $name => $value) {
            if (property_exists($obj, $name)) {
                $obj->$name = $value;
            }
        }
    }
}
                                
/**
 * 
 * p输出有规则的数据，用于调试
 * @param 传入的值
 * @param 是否结束
 */
if (!function_exists('p')) {
    function p($value, $is_end = '') {
        echo '<pre>';
        print_r($value);
        echo '</pre>';
        if (!empty($is_end)) {
            die();
        }

    }
}

    /**
     * 判断远程URL图片是否存在
     * @param type $url
     * @return boolean
     */
if(!function_exists('checkImg')) {
    function checkImg($url) {
        $head = get_headers($url);
        $str = $head[0];
        if (strpos($str, '200')) {
            return $url;
        }
        return static_file('m/img/peop.png');
    }
}
                                
if (!function_exists('error_all')) {        
    function error_all($errno, $errstr) {
        throw new Exception($errstr);
    }
}
if (!function_exists('stringLength')) {  
    function stringLength($str){
        if (isAscii($str))
        return strlen($str);
        return strlen(utf8_decode($str));
    }
}

if (!function_exists('isAscii')) {
    function isAscii($str){  
        return ! preg_match('/[^\x00-\x7F]/S', $str);
    }
}

//字符串的截取，包括中英文
if (!function_exists('msubstr')) {  
    function msubstr($str, $start = 0, $length, $charset = "utf-8", $suffix = true) {
        if (function_exists("mb_substr"))
            $slice = mb_substr($str, $start, $length, $charset);
        elseif (function_exists('iconv_substr')) {
            $slice = iconv_substr($str, $start, $length, $charset);
        } else {
            $re['utf-8'] = "/[\x01-\x7f]|[\xc2-\xdf][\x80-\xbf]|[\xe0-\xef][\x80-\xbf]{2}|[\xf0-\xff][\x80-\xbf]{3}/";
            $re['gb2312'] = "/[\x01-\x7f]|[\xb0-\xf7][\xa0-\xfe]/";
            $re['gbk'] = "/[\x01-\x7f]|[\x81-\xfe][\x40-\xfe]/";
            $re['big5'] = "/[\x01-\x7f]|[\x81-\xfe]([\x40-\x7e]|\xa1-\xfe])/";
            preg_match_all($re[$charset], $str, $match);
            $slice = join("", array_slice($match[0], $start, $length));
        }

        $str_len = mb_strlen($str, $charset);
        if ($suffix == false) {
            return $slice;
        }
        return $slice;
    }
}

/**
 * 提示消息
 * @param  boolean $msg m=>消息级别 1-4 title=>标题  msg=> 消息
 * @return [type]       [description]
 */
function notify_msg($msg= false,$aid=false)
{
	logfile($msg, 'NOTICE_MSG');
    // n 类型
    // 1 充值操作
    // 2 提现操作
    // 3 奖励兑换
    // 4 领取任务

    // m 级别
    // 1 系统
    // 2 推送
    // 3 短信
    // 4 微信

    if ($msg == false or !array_key_exists('m', $msg) or !array_key_exists('title', $msg)  or  !array_key_exists('msg', $msg)) {
        return false;
    }

    $CI = & get_instance();

    if (!$aid) {
    	return false;
    }

    $retrun = array(true,true,true);
    $notify = explode(',', $msg['m']);

    // 系统消息
    if (in_array(1,$notify)) {
        $CI->load->model('notice_model', 'mnotice');

        if (is_numeric($aid)) {
        	
        	// 处理单挑数据
        	$CI->mnotice->create(
	            array(
	                'toer'      => $aid
	                ,'title'    => $msg['title']
	                ,'content'  => $msg['msg']
	                ,'timeline' => time()
	            )
	        );
        } else {

        	// 处理多条数据 11,22,33,44
        	$aids = explode(',', $aid);
        	$batch_data = array();
        	foreach ($aids as $id) {
        		$tmp = array();
        		$tmp['toer']    = $id;
        		$tmp['title']   = $msg['title'];
        		$tmp['content'] = $msg['msg'];
        		$tmp['timeline']= time();
        		array_push($batch_data, $tmp);
        	}

        	$CI->mnotice->create($batch_data, TRUE);
        }
        
    }

    // 推送消息
    if (in_array(2,$notify)) {
        $CI->load->model('jpush_msg_model', 'mjpush');
        $CI->mjpush->create(
            array(
                'aid'      => $aid
                ,'title'   => $msg['title']
                ,'msg'     => $msg['msg']
                ,'extras'  => $msg['extras']
                ,'timeline'=> time()
            )
        );
    }

    // 短信消息
    if (in_array(3,$notify)) {
        // 短信消息
    }

     // 微信消息
    if (in_array(4,$notify)) {
        // 模版消息推送
    }

    return $retrun;
}

